package com.skin.admin.controller;

import com.skin.admin.common.FenyeSetting;
import com.skin.admin.entity.Fenye;
import com.skin.admin.entity.Goods;
import com.skin.admin.manager.GoodsManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author skin
 * @since 2018-08-03
 */
@Controller
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsManager goodsManager;

    @RequestMapping({"/","/index"})
    public String index(HttpServletRequest request,Model model){
        Fenye f= FenyeSetting.setfenye(request);
        model.addAttribute("goods",goodsManager.All(f.getCurrent(),f.getSize()));
        return "goods/index";
    }

    @RequestMapping("/insert")
    public String insert(){
        return "goods/goodsinsert";
    }

    @RequestMapping(value = "/insertDo",method = RequestMethod.POST)
    @ResponseBody
    public Map insertDo(Goods goods){
        Map map=new HashMap<>();
        map.put("status",0);
        boolean b =goodsManager.insert(goods);
        if(b){
            map.put("status",1);
            map.put("result",goods.getId().toString());
            return map;
        }
        return map;
    }

    @RequestMapping("/delete")
    public String delete(@RequestParam("id") String id){
//        goodsManager.deletebyid(id);
        return "redirect:goods/index";
    }
}

